using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml;
using System.Xml.XPath;

namespace Graphs
{
	class GraphInit
	{
		public static CGraph Init1()
		{
			CGraph graph = new CGraph();

			System.IO.TextReader tr = new System.IO.StreamReader("graph.txt");

			string line;
			int k = 0;
			while ((line = tr.ReadLine()) != null)
			{
				string [] points = line.Split('-');
				if (points.Length == 2)
				{
					string [] point1 = points [0].Split(',');
					string [] point2 = points [1].Split(',');

					Point node1Loc = new Point(Convert.ToInt32(point1 [0]), Convert.ToInt32(point1 [1]));
					Point node2Loc = new Point(Convert.ToInt32(point2 [0]), Convert.ToInt32(point2 [1]));

					Console.WriteLine("{0} {1}", node1Loc, node2Loc);
					CNode node1 = graph.GetNode(node1Loc);
					CNode node2 = graph.GetNode(node2Loc); ;


					if (node1 == null)
					{
						k++;
						node1 = new CNode("N" + k.ToString(), node1Loc);
						graph.AddNode(node1);
					}
					if (node2 == null)
					{
						k++;
						node2 = new CNode("N" + k.ToString(), node2Loc);
						graph.AddNode(node2);
					}


					graph.AddEdge(node1, node2);

				}
			}



			/*List<CNode> nodes = new List<CNode>();
			for (int i = 0; i < 10; i++)
			{
				//nodes.Add(new CNode(i.ToString(),new Point(
			}

			graph.AddEdge(new CNode("nod1", new Point(10, 10)), new CNode("nod2", new Point(20, 30)));
			graph.AddEdge(graph.GetEdge(0).StartNode, new CNode("nod3", new Point(50, 20)));
			graph.AddEdge(graph.GetEdge(0).EndNode, graph.GetEdge(1).EndNode);
			  */



			return graph;


		}

		public static CGraph Init2()
		{
			CGraph graph = new CGraph();
			int k = 0;
			Random r = new Random();
			int nrn = 100;

			for (int i = 0; i < nrn; i++)
			{
				Point node1Loc = new Point(r.Next(0, 600), r.Next(0, 600));
				Point node2Loc = new Point(r.Next(0, 600), r.Next(0, 600));



				CNode node1 = graph.GetNode(node1Loc);
				CNode node2 = graph.GetNode(node2Loc); ;


				if (node1 == null)
				{
					k++;
					node1 = new CNode("N" + k.ToString(), node1Loc);
					graph.AddNode(node1);
				}
				if (node2 == null)
				{
					k++;
					node2 = new CNode("N" + k.ToString(), node2Loc);
					graph.AddNode(node2);
				}



			}

			for (int i = 0; i < 2 * graph.Nodes.Count; i++)
			{



				graph.AddEdge(graph.Nodes [r.Next(graph.Nodes.Count)], graph.Nodes [r.Next(graph.Nodes.Count)]);
			}







			return graph;
		}





		public static CGraph Init3()
		{
			CGraph graph = new CGraph();
			XmlDocument x = new XmlDocument();
			//x.Load("ctza.xml");
			x.Load(@"D:\My Downloads\romania.osm\romania.osm");


			XmlNodeList cNodes = x.SelectNodes("/osm/node");

			foreach (XmlNode xn in cNodes)
			{
				float lat = (float) Convert.ToDouble(xn.Attributes ["lat"].Value);
				float lon = (float) Convert.ToDouble(xn.Attributes ["lon"].Value);
				string id = xn.Attributes ["id"].Value;

				CNode node = new CNode(id, new PointF(lat, lon));
				graph.AddNode(node);
			}

			Console.WriteLine(graph.Nodes.Count);

			XmlNodeList ways = x.SelectNodes("//way");

			foreach (XmlNode xn in ways)
			{
				string id = xn.Attributes ["id"].Value;
				XmlNodeList nodes = xn.SelectNodes("nd");
				//Console.WriteLine(nodes.Count);


				CNode wNode = null;
				CNode wNode2 = null;

				foreach (XmlNode xn2 in nodes)
				{
					CEdge edge;
					string refid = xn2.Attributes ["ref"].Value;

					wNode2 = wNode;
					wNode = graph.GetNode(refid);
					if (wNode2 != null)
					{
						edge = new CEdge(wNode2, wNode);
						graph.AddEdge(edge);
					}
				}
			}
			Console.WriteLine(ways.Count);
			return graph;

		}




		public static CGraph Init4()
		{
			Console.WriteLine(DateTime.Now);

			CGraph graph = new CGraph();
			XPathDocument xpd = new XPathDocument(@"D:\My Downloads\romania.osm\romania.xml");
			XPathNavigator xpn = xpd.CreateNavigator();
			XPathNodeIterator xpni = xpn.Select("/osm/node");
			long k = 0;
			while (xpni.MoveNext())
			{
				
				float lat = (float) Convert.ToDouble(xpni.Current.GetAttribute("lat",""));
				float lon = (float) Convert.ToDouble(xpni.Current.GetAttribute("lon", ""));
				string id = xpni.Current.GetAttribute("id", "");

				CNode node = new CNode(id, new PointF(lon, lat));
				graph.AddNode(node);
				k++;

			}

			Console.WriteLine(k);

			Console.WriteLine(DateTime.Now);
			Console.WriteLine(graph.Nodes.Count);

			return graph;
		}










	}
}
